package service

import (
	"context"
	"errors"
	__ "order_srv/basic/orderproto"
	"order_srv/handler/dao"
)

// 订单疑义添加
func (s *Server) DoubtAdd(_ context.Context, in *__.DoubtAddReq) (*__.DoubtAddResp, error) {
	add, err := dao.DoubtAdd(in)
	if err != nil {
		return nil, errors.New(err.Error())
	}

	return &__.DoubtAddResp{
		Id: add.Id,
	}, nil
}

// 订单疑义删除
func (s *Server) DoubtDel(_ context.Context, in *__.DoubtDelReq) (*__.DoubtDelResp, error) {
	_, err := dao.DoubtDel(in)
	if err != nil {
		return nil, errors.New(err.Error())
	}
	return &__.DoubtDelResp{}, nil
}

// 订单疑义修改
func (s *Server) DoubtUpdate(_ context.Context, in *__.DoubtUpdateReq) (*__.DoubtUpdateResp, error) {
	if in.Id <= 0 {
		return nil, errors.New("参数错误")
	}
	_, err := dao.DoubtUpdate(in)
	if err != nil {
		return nil, errors.New(err.Error())
	}
	return &__.DoubtUpdateResp{
		Id: in.Id,
	}, nil
}

// 订单疑义列表
func (s *Server) DoubtList(_ context.Context, in *__.DoubtListReq) (*__.DoubtListResp, error) {
	list, err := dao.DoubtList(in)
	if err != nil {
		return nil, errors.New(err.Error())
	}
	return &__.DoubtListResp{
		List: list,
	}, nil
}

// 订单疑义详情
func (s *Server) DoubtDetail(_ context.Context, in *__.DoubtDetailReq) (*__.DoubtDetailResp, error) {
	if in.Id <= 0 {
		return nil, errors.New("参数错误")
	}

	detail, err := dao.DoubtDetail(in)
	if err != nil {
		return nil, errors.New(err.Error())
	}
	StartTime := detail.StartTime.Format("2006-01-02 15:04:05")
	EndTime := detail.EndTime.Format("2006-01-02 15:04:05")
	return &__.DoubtDetailResp{
		Id:             detail.Id,
		OrderId:        detail.OrderId,
		UserId:         detail.UserId,
		PreviousAmount: float32(detail.PreviousAmount),
		LaterAmount:    float32(detail.LaterAmount),
		StartTime:      StartTime,
		EndTime:        EndTime,
		AdjustmentType: uint64(detail.AdjustmentType),
		Reason:         detail.Reason,
	}, nil
}

// 订单疑义修改状态
func (s *Server) DoubtUpdateStatus(_ context.Context, in *__.DoubtUpdateStatusReq) (*__.DoubtUpdateStatusResp, error) {
	if in.Id <= 0 {
		return nil, errors.New("id参数错误")
	}
	if in.Status <= 0 {
		return nil, errors.New("状态参数错误")
	}
	detail, err := dao.DoubtUpdateStatus(in)
	if err != nil {
		return nil, errors.New(err.Error())
	}
	return &__.DoubtUpdateStatusResp{
		Id: detail.Id,
	}, nil
}
